
<?php

function check_username($username)
{
	return preg_match('/^[a-z0-9]{5,30}$/', $username);
}

function check_device($deviceid)
{
	return preg_match('/^[0-9a-zA-Z\\.\\-\\_]{10,60}$/', $deviceid);
}

function check_app($app)
{
	return preg_match('/^[A-Za-z0-9\\.\\-\\_]{1,60}$/', $app);
}

function check_os($os)
{
	return preg_match('/^[A-Za-z0-9\\.\\-\\_]{1,60}$/', $os);
}

function check_token($token)
{
	return preg_match('/^[0-9A-Za-z]{30,60}$/', $token);
}

function check_name($name)
{
	return strlen($name) >= 5 && strlen($name) < 40;
}

function get_request_ip()
{
	if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
		return $_SERVER["HTTP_X_FORWARDED_FOR"];
	} elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
		return $_SERVER["HTTP_CLIENT_IP"];
	} elseif (isset($_SERVER["REMOTE_ADDR"])) {
		return $_SERVER["REMOTE_ADDR"];
	} else {
		return 'UNKNOWN';
	}
}

function gen_token($len = 32, $md5 = true)
{
	# Seed random number generator
	# Only needed for PHP versions prior to 4.2
	mt_srand((double) microtime() * 1000000);
	
	# Array of characters, adjust as desired
	$chars = array(
		'Q', '@', '8', 'y', '%', '^', '5', 'Z', '(', 'G', '_', 'O', '`',
		'S', '-', 'N', '<', 'D', '{', '}', '[', ']', 'h', ';', 'W', '.',
		'/', '|', ':', '1', 'E', 'L', '4', '&', '6', '7', '#', '9', 'a',
 		'A', 'b', 'B', '~', 'C', 'd', '>', 'e', '2', 'f', 'P', 'g', ')',
		'?', 'H', 'i', 'X', 'U', 'J', 'k', 'r', 'l', '3', 't', 'M', 'n',
		'=', 'o', '+', 'p', 'F', 'q', '!', 'K', 'R', 's', 'c', 'm', 'T',
		'v', 'j', 'u', 'V', 'w', ',', 'x', 'I', '$', 'Y', 'z', '*'
	);
	
	# Array indice friendly number of chars; empty token string
	$numChars = count($chars) - 1;
	$token    = '';
	
	# Create random token at the specified length
	for ($i = 0; $i < $len; $i++)
		$token .= $chars[mt_rand(0, $numChars)];
	
	# Should token be run through md5?
	if ($md5) {
		# Number of 32 char chunks
		$chunks   = ceil(strlen($token) / 32);
		$md5token = '';
		
		# Run each chunk through md5
		for ($i = 1; $i <= $chunks; $i++)
			$md5token .= md5(substr($token, $i * 32 - 32, 32));
		
		# Trim the token
		$token = substr($md5token, 0, $len);
	}
	return $token;
}

function get_params($params)
{
	if ($_SERVER['REQUEST_METHOD'] == 'GET') {
		$args = $_GET;
	} else {
		$args = $_POST;
	}
	
	$t_ret = Array();
	foreach ($params as $key => $val) {
		if (isset($args[$key])) {
			switch (substr($val, 1)) {
				case 'any':
					break;
				case 'username':
					if (!check_username($args[$key]))
						return false;
					break;
				case 'device':
					if (!check_device($args[$key]))
						return false;
					break;
				case 'app':
					if (!check_app($args[$key]))
						return false;
					break;
				case 'os':
					if (!check_os($args[$key]))
						return false;
					break;
				case 'token':
					if (!check_token($args[$key]))
						return false;
					break;
				case 'name':
					if (!check_name($args[$key]))
						return false;
					break;
				case 'number':
					if (!is_numeric($args[$key]))
						return false;
					break;
				case 'avatar':
					if (strlen($args[$key]) > 600)
						return false;
					break;
				case 'savafile':
					if (strlen($args[$key]) > 600)
						return false;
					break;
				default:
					error_log('invalid match mode - ' . $val, 0);
					return false;
			}
			$t_ret[$key] = $args[$key];
		} else {
			if (substr($val, 0, 1) == '+') {
				return false;
			}
			$t_ret[$key] = '';
		}
	}
	
	return $t_ret;
}

?>
